package com.imooc.reader.portal.controller;

import com.imooc.reader.portal.dto.ResponseDTO;
import com.imooc.reader.portal.entity.Member;
import com.imooc.reader.portal.entity.MemberReadState;
import com.imooc.reader.portal.service.MemberService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/members")
public class MemberController {
    @Resource
    private MemberService memberService;
    @PostMapping("/verification_codes")
    public ResponseDTO sendVCode(String mobile){
        memberService.sendVCode(mobile);
        return new ResponseDTO();
    }
    @PostMapping("")
    public ResponseDTO register(String mobile,String vcode){
        Member member = memberService.register(mobile, vcode);
        return new ResponseDTO().put("member", member);
    }

    @GetMapping("/{mid}")
    public ResponseDTO selectById(@PathVariable("mid") Long memberId){
        return new ResponseDTO().put("member", memberService.selectById(memberId));
    }

    @GetMapping("/{mid}/read_state")
    public ResponseDTO selectMemberReadState(@PathVariable("mid") Long memberId, Long bookId) {
        MemberReadState memberReadState = memberService.selectMemberReadState(memberId, bookId);
        return new ResponseDTO().put("readState", memberReadState);
    }

    @PutMapping("/{mid}/read_state")
    public ResponseDTO updateReadState(@PathVariable("mid") Long memberId, Long bookId, Integer readState) {
        MemberReadState memberReadState = memberService.updateMemberReadState(memberId, bookId, readState);
        return new ResponseDTO().put("readState", memberReadState);
    }
}
